import 'package:flutter_shop/pay/alipay/ali_pay_result_data.dart';
import 'package:flutter_shop/utils/log_utils.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:tobias/tobias.dart';

///Title:支付宝支付 <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-03-05 <br/>
///version 1.0 <br/>
class AliPay {
  static const String TAG = "AliPay";

  factory AliPay() => _getInstance();

  static AliPay _instance;

  static AliPay _getInstance() {
    if (_instance == null) {
      _instance = AliPay._internal();
    }
    return _instance;
  }

  AliPay._internal() {}

  ///判断支付宝是否安装
  Future<bool> isAliPayInstall() async {
    return isAliPayInstalled();
  }

  ///调用支付宝支付
  Future<AliPayResultData> callPay(String order) async {
    AliPayResultData result = AliPayResultData();

    if (!await AliPay().isAliPayInstall()) {
      result.memo = "支付宝未安装";
      result.resultStatus = "4000";
      return result;
    }

    try {
      Map resultMap = await aliPay(order);
      String resultStr = Utils.parseObj2Json(resultMap);
      Log.d(resultStr, tag: TAG);
      Utils.parseJson2Obj<AliPayResultData>(resultStr, (v) => result.fromJson(v));
    } catch (e) {
      print(e);
      result.memo = "调用支付异常";
      result.resultStatus = "4000";
    }
    return result;
  }
}
